import Cookies from "js-cookie";
import CryptoJS from "crypto-js"
const TokenKey = "IDVDESKTOP_TOKEN_KEY";
const AuthmodeKey = "IDVDESKTOP_AUTHMODE_KEY";
import i18n from "@/lang";


// 在浏览器的本地存储中记录和读取配置信息
export const Token = {
  get: () => localStorage.getItem(TokenKey),
  set: (token: string) => localStorage.setItem(TokenKey, token),
  delete: () => localStorage.removeItem(TokenKey)
};

export const Authmode = {
  get: () => localStorage.getItem(AuthmodeKey),
  set: (authmode: string) => localStorage.setItem(AuthmodeKey, authmode),
  delete: () => localStorage.removeItem(AuthmodeKey)
};

export const Language = {
  get: () => Cookies.get("language") || "zh",
  set: (L: string) => Cookies.set("language", L)
};

export const ContentTypes = {
  get: () => Cookies.get("content-types") || "{}",
  set: (a: string) => Cookies.set("content-types", a)
};

export const ThemeSettings = {
  get: () => Cookies.get("theme-settings") || "{}",
  set: (a: string) => Cookies.set("theme-settings", a)
};

export const FeaturesConfig = {
  get: () => Cookies.get("features-config") || "{}",
  set: (a: string) => Cookies.set("features-config", a)
};

export const DesktopPoolList = {
  get: () => localStorage.getItem("desktop-pool-list") || "[]",
  set: (d: string) => localStorage.setItem("desktop-pool-list", d)
};

export const decodeToken = (token: string) => {
  const key = CryptoJS.enc.Utf8.parse("9999999999999999");
  const iv = CryptoJS.enc.Utf8.parse("qqqqqqqqqqqqqqqq");

  const hexToken = CryptoJS.enc.Hex.parse(token);
  const b64Token = CryptoJS.enc.Base64.stringify(hexToken);

  const destr = CryptoJS.AES.decrypt(b64Token, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    // padding: CryptoJS.pad.ZeroPadding,
  }).toString(CryptoJS.enc.Utf8).toString();
  if (destr) {
    let str: string = JSON.stringify(destr);
    str = JSON.parse(str.replace(/\\u0000/g,"")); // 去除所有换行*)
    return str;
  } else {
    return token;
  } // 未加密时直接使用
}

/*
* 对密码进行加密，传输给后台进行验证
* @param  {String}     word    需要加密的密码
* @return {String}     加密的密文
* */
export const encrypt = (word: any) => { // 加密
  const key = CryptoJS.enc.Utf8.parse('9999999999999999');
  const iv = CryptoJS.enc.Utf8.parse("qqqqqqqqqqqqqqqq");
  let srcs = CryptoJS.enc.Utf8.parse(word);
  let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
  return encrypted.ciphertext.toString();
}
/*
* 对加密之后的密文在页面上进行解密，以便用户进行修改
* @param  {String}     word    需要解密的密码
* @return {String}      解密的明文
* */
export const decrypt = (word: any) => { // 解密
  const key = CryptoJS.enc.Utf8.parse("9999999999999999");
  const iv = CryptoJS.enc.Utf8.parse("qqqqqqqqqqqqqqqq");

  const hexStr = CryptoJS.enc.Hex.parse(word);
  const b64Str = CryptoJS.enc.Base64.stringify(hexStr);

  let decrypt = CryptoJS.AES.decrypt(b64Str, key, {
    mode: CryptoJS.mode.CBC,
    iv: iv,
    // padding: CryptoJS.pad.ZeroPadding
  }).toString(CryptoJS.enc.Utf8).toString().replace(/\x00/g, '');
  return decrypt;
}

export const getCpus =(cpus: any, memory_mb: any) => {
  let str = '';
  if (cpus) {
    if (memory_mb) {
      str = cpus + i18n.t('desktopManage.cpu').toString() + memory_mb / 1024 + 'G'
    } else {
      str = cpus + i18n.t('desktopManage.cpu').toString() + cpus*2 + 'G'
    }
  } else {
    if (memory_mb) {
      str = ((memory_mb / 1024) / 2) + i18n.t('desktopManage.cpu').toString() + memory_mb / 1024 + 'G'
    } else {
      str = ''
    }
  }
  return str;
}
